/**
 * 
 */
(function($){
	
	 var artHtml='<div class="myScroll_up">'+
						'<table class="myScroll_table">'+
						'</table>'+
					'</div>'+
					'<div class="myScroll_down">'+
				'</div>';
	 var  p ='<p><a href="javascript:void(0);" ></a><a href="javascript:void(0);" class="btn_lh"></a><em></em></p>'
     var p2 ='<p><a href="javascript:void(0);" class="a_blue"></a><span></span></p>'
	 
	 
	 
	
	function create(target){
		$(target).empty();
		var opts  = $.data(target,'myScroll').options;
		$(target).addClass("myScroll");
		var  p = $(target).parent();
		$(target).width(p.width());
		$(target).height(p.height());
		doAjax(target);
	}
	 
	function doAjax(target){
		var options = $.data(target,'myScroll').options;
		//根据数据 渲染页面
		if(options.url){
			var queryParams= $.extend({},options.queryParams,{rows:options.rows,page:1});
            options.onBeforeAjax(queryParams);
			$.utils.request(options.url,queryParams,null,options.method,null,function(data){
				var formatData  = options.dataFilter(data);
				createView(target,formatData);
			});
		}else{
			var data =options.data;
			createView(target,data);
		}
	}
	 
	function createView(target,data){
		var ul = $('<ul></ul>').appendTo(target)
		  // for(var i=1;i<10;i++)
			for(var p in data){
				var r = data[p];
				var li = $('<li></li>').appendTo(ul)
				li.append('<p><span class="fh">'+r.nHid+'</span>  <span class="dh">'+r.nPhone+'</span></p>');
				li.append('<span class="nr">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+r.nContent+'</p>');
				var obj = new Date();
				 obj.setTime(r.createTime);
				 var dstr = obj.getFullYear()+"-"+(obj.getMonth()+1)+"-"+obj.getDate();
				li.append('<span class="time">创建时间：'+dstr+'</p>');
			}
				
		$(target).scroll({
			speed:62, //数值越大，速度越慢
			rowHeight:140 //li的高度
		});
	}
	
	
	$.fn.scroll = function(options){
		//默认配置
		var defaults = {
			speed:40,  //滚动速度,值越大速度越慢
			rowHeight:130 //每行的高度
		};
		
		var opts = $.extend({}, defaults, options),intId = [];
		
		function marquee(obj, step){
		
			obj.find("ul").animate({
				marginTop: '-=1'
			},0,function(){
					var s = Math.abs(parseInt($(this).css("margin-top")));
					if(s >= step){
						$(this).find("li").slice(0, 1).appendTo($(this));
						$(this).css("margin-top", 0);
					}
				});
			}
			
			this.each(function(i){
				var sh = opts["rowHeight"],speed = opts["speed"],_this = $(this);
				speed=62;
				intId[i] = setInterval(function(){
					if(_this.find("ul").height()<=_this.height()){
						clearInterval(intId[i]);
					}else{
						marquee(_this, sh);
					}
				}, speed);

				_this.hover(function(){
					clearInterval(intId[i]);
				},function(){
					intId[i] = setInterval(function(){
						if(_this.find("ul").height()<=_this.height()){
							clearInterval(intId[i]);
						}else{
							marquee(_this, sh);
						}
					}, speed);
				});
			
			});

		}
	
	
	$.fn.myScroll=function(options,params){
		if(typeof options == "string"){
			return $.fn.myScroll.methods[options](this,params);
		}
		options = options||{};
		return this.each(function(){
			var state= $.data(this,'myScroll');
			if(state){
				$.extend(state.options, options);
			}else{
				$.data(this, 'myScroll', {
					options: $.extend({}, $.fn.myScroll.defaults,$.fn.myScroll.parseOptions(this),options)
				});
			}
			create(this);
		});
	}
	
	$.fn.myScroll.defaults={
			dataFilter:function(data){
				return data.rows;
			},
			onBeforeAjax:function(params){
				
			}
	};
	
	$.fn.myScroll.parseOptions=function(target,options){
		return {}
	};
	$.fn.myScroll.methods={
	
	}
})(jQuery);